package com.zls.controller;

import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.page.Result;
import com.zls.pojo.Project;
import com.zls.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/project")
@RestController
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @GetMapping("findAll")
    public List<Project> findAll(){
        List<Project> all = projectService.findAll();
        return all;
    }

    /**
     * 工程列表查询
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult page = projectService.findPage(queryPageBean);
        return page;
    }

    /**
     * 工程列表添加
     */
    @PostMapping("add")
    public Result add(@RequestBody Project project){
        int i = projectService.addProject(project);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
    /**
     * 工程列表删除
     */
    @GetMapping("delete")
    public Result delete(@RequestParam("pid")int pid){
        int i = projectService.deleteProject(pid);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    /**
     * 查询修改工程列表
     */
    @GetMapping("findById")
    public Result findById(@RequestParam("pid") int pid){
        Project project=projectService.findById(pid);
        Result result=new Result();
        if (project!=null){
            result.setData(project);
            result.setFlag(true);
        }else {
            result.setMessage("系统错误");
        }
        return result;
    }
    /**
     * 修改工程列表
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Project project){
        int i = projectService.editProject(project);
        if (i>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }
}
